#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h> //目录名获取配合DIR
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h> //文件控制操作
#include <sys/types.h> //获取pid等
#include <sys/wait.h> //等待子进程结束
#include <time.h> //时间获取
#include <sys/ipc.h> //进程通信
#include <sys/msg.h> //消息队列
#include <sys/shm.h> //共享内存
#include <signal.h> //信号处理
#include <pthread.h> //线程
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "cJSON.h"

struct msg {
    char operator[10];
    int number1;
    int number2;
    int result;
};
void fun2(int sig) {
    exit(0);
}
int main(int argc, char *argv[])
{
    signal(SIGINT, fun2);
    int temp = 0;
    int sock = 0;
    int ret = 0;
    struct sockaddr_in serAddr;
    memset(&serAddr, 0, sizeof(serAddr));

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock == -1)
    {
        perror("sock");
        exit(-1);
    }

    serAddr.sin_family = AF_INET;
    serAddr.sin_port = htons(5791);
    serAddr.sin_addr.s_addr = inet_addr("192.168.48.128");
    while (1)
    {
        struct msg k;
        /*
        printf("请输入运算符：(加1，减2，乘3，除4)\n");
        scanf("%d", &temp);
        if (temp == 1)
        {
            strcpy(k.operator, "add");
        }
        else if (temp == 2)
        {
            strcpy(k.operator, "sub");
        }
        else if (temp == 3)
        {
            strcpy(k.operator, "mul");
        }
        else if (temp == 4)
        {
            strcpy(k.operator, "div");
        }
        else
        {
            printf("输入有误！\n");
            return 0;
        }
        printf("请输入第一个数：\n");
        scanf("%d", &k.number1);
        printf("请输入第二个数：\n");
        scanf("%d", &k.number2);
        */
        srand(time(NULL)); 
        for (int i = 0; i < 10; i++) {
            int temp = rand() % 4 + 1; 
            if (temp == 1) {
                strcpy(k.operator, "add");
            }
            else if (temp == 2) {
                strcpy(k.operator, "sub");
            }
            else if (temp == 3) {
                strcpy(k.operator, "mul");
            }
            else if (temp == 4) {
                strcpy(k.operator, "div");
            }

            k.number1 = rand() % 10;
            k.number2 = rand() % 10;



            cJSON* json = NULL;
            json = cJSON_CreateObject();
            cJSON_AddStringToObject(json, "operator", k.operator);
            cJSON_AddNumberToObject(json, "number1", k.number1);
            cJSON_AddNumberToObject(json, "number2", k.number2);
            char* dest = NULL;
            dest = cJSON_Print(json);
            printf("发送的字符串是%s\n", dest);
            ret = sendto(sock, dest, strlen(dest), 0, (struct sockaddr*)&serAddr, sizeof(serAddr));
            if (ret == -1)
            {
                perror("sendto");
                exit(-1);
            }
            cJSON_Delete(json);

        }
        //收到的结果
        printf("等待接收数据----------------------\n");
        while (1) {
            char buf[1024] = { 0 };
            memset(buf, 0, sizeof(buf));
            ret = recvfrom(sock, buf, sizeof(buf), 0, NULL, NULL);//发过来的是字符串，注意接受格式！！
            if (ret == -1)
            {
                perror("recvfrom");
                exit(-1);
            }
            printf("收到的结果是：%s\n", buf);
            struct msg res;
            cJSON* json2 = NULL;
            json2 = cJSON_Parse(buf);
            if (json2 == NULL) {
                printf("解析error\n");
            }
            cJSON* num = NULL;
            num = cJSON_GetObjectItem(json2, "result");
            if (num == NULL) {
                printf("读取error\n");
            }
            if (num->type == cJSON_Number) {
                res.result = num->valueint;
                printf("结果为：%d\n", res.result);
            }
        }
    }
    close(sock);
    return 0;
}






